<template>
	<view class="add_shop">
		<u-toast ref="uToast"></u-toast>
		<uni-forms :value="formData" ref="form" :rules="rules">

			<uni-forms-item name="shopName" class="form_item">
				<view class="form_item_box">
					<text>商品名称</text>
					<input type="text" placeholder-style="margin-left:20rpx" v-model="formData.shopName"
						@input="binddata('shopName',$event.detail.value)" />
				</view>
			</uni-forms-item>

			<uni-forms-item name="shopCode" class="form_item">
				<view class="form_item_box">
					<text>商品编号</text>
					<input type="text" placeholder-style="margin-left:20rpx" v-model="formData.shopCode"
						@input="binddata('shopCode',$event.detail.value)" />
				</view>
			</uni-forms-item>

			<uni-forms-item name="shopPrice" class="form_item">
				<view class="form_item_box">
					<text>商品价格</text>
					<input type="text" placeholder-style="margin-left:20rpx" v-model="formData.shopPrice"
						@input="binddata('shopPrice',$event.detail.value)" />
				</view>
			</uni-forms-item>

			<!-- <uni-forms-item name="userTypeName" class="form_item">
				<view class="form_item_box">
					<text>商品价格</text>
					<picker @change="typePickerChange" :value="selectIndex" :range="typeArr" range-key="text">
						<input type="text" v-model="formData.userTypeName"
							@input="binddata('userTypeName',$event.detail.value)" disabled>
					</picker>
					<image class="row" src="../../static/images/row.png" mode=""></image>
				</view>
			</uni-forms-item> -->
			
			<uni-forms-item name="shopSize" class="form_item">
				<view class="form_item_box">
					<text>商品码数</text>
					<u-checkbox-group
					    class="checkbox-groud"
						v-model="formData.shopSize"
						placement="row"
						@change="checkboxChange"
						activeColor="#cd5959"
					>
						<u-checkbox
							:customStyle="{marginBottom: '8px'}"
							v-for="(item, index) in checkboxList1"
							:key="index"
							:label="item.name"
							:name="item.name"
						>
						</u-checkbox>
					</u-checkbox-group>
				</view>
			</uni-forms-item>
			
			<uni-forms-item name="shopColor" class="form_item">
				<view class="form_item_box">
					<text>商品颜色</text>
					<u--textarea v-model="formData.shopColor" placeholder="请输入内容" autoHeight></u--textarea>
				</view>
			</uni-forms-item>

			<uni-forms-item name="imageUrls" class="form_item upload_form_item">
				<view class="form_item_box upload_box">
					<view class="upload_title">商品图片</view>
					<view class="">
						<u-upload :fileList="fileList1" :maxCount="4" multiple @afterRead="afterRead"
							@delete="deletePic">
						</u-upload>
					</view>
				</view>
			</uni-forms-item>

			<button @click="submitForm" class="form_btn">提交申请</button>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					shopName: '',
					shopCode: '',
					shopPrice: '',
					shopSize: [],
					shopColor:'',
					// cover: 'https://cdn.uviewui.com/uview/swiper/swiper3.png',
					imageUrls:[]
				},
				// 基本案列数据
				checkboxList1: [{
						name: '均码',
						disabled: false
					},
					{
						name: 'S',
						disabled: false
					},
					{
						name: 'M',
						disabled: false
					},
					{
						name: 'L',
						disabled: false
					},
					{
						name: 'XL',
						disabled: false
					},
					{
						name: 'XXL',
						disabled: false
					},
					{
						name: 'XXXL',
						disabled: false
					},
				],
				typeArr: [{
						text: '提货',
						value: '0'
					},
					{
						text: '配送',
						value: '1'
					}
				],
				selectIndex: 0,
				rules: {
					shopName: {
						rules: [{
							required: true,
							errorMessage: '请填写商品名称',
						}]
					},
					shopCode: {
						rules: [{
							required: true,
							errorMessage: '请填写商品编号',
						}]
					},
					shopPrice: {
						rules: [{
							required: true,
							errorMessage: '请填写商品价格',
						}]
					},
					shopSize: {
						rules: [{
							required: true,
							errorMessage: '请填写商品码数',
						}]
					},
					shopColor: {
						rules: [{
							required: true,
							errorMessage: '请填写商品颜色',
						}]
					},
					// cover: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择预约时间',
					// 	}]
					// },
				},
				fileList1: [],
			}
		},
		methods: {
			checkboxChange(n) {
					console.log('change', n);
				},
			submitForm(form) {
				
				// if(this.fileList1.length === 0) {
				// 	this.$refs.uToast.show({
				// 		type: 'error',
				// 		message:'请上传图片',
				// 	})
				// }
				
				
				
				
				this.$refs.form.validate().then(async () => {
					
					uni.showLoading({
					    title: '加载中'
					});
					this.formData.imageUrls = []
					console.log(1,this.fileList1)
					for(let i = 0; i < this.fileList1.length; i++){
						const result = await this.uploadFilePromise(this.fileList1[i].url)
						this.fileList1[i].url = result
						this.formData.imageUrls.push(result)
					}
					console.log(2,this.fileList1)
					
					
					
					
					
					this.formData.shopColor = this.formData.shopColor.split(',')
					console.log('添加3', this.formData)
					uniCloud.callFunction({
						name: 'addShop',
						// 新增
						data: this.formData,
						success: (res) => {
							uni.hideLoading();
							console.log('成功了', res)
							if (res.result.res_code === 0) {
								this.$refs.uToast.show({
									type: 'error',
									message: res.result.res_message,
								})
								uni.navigateBack({
									
								})
							} else {
								uni.hideLoading();
								uni.navigateBack({
									
								})
								// uni.switchTab({
								// 	url: '../index/index',
								// 	// success: function(e) {
								// 	// 	let page = getCurrentPages().pop();
								// 	// 	if (page == undefined || page == null) return;
								// 	// 	page.onLoad();
								// 	// }
								// })
								
							}
						

						},
						fail: (err) => {
							console.log('失败了', err)
						}
					})
				})
			},
			typePickerChange(e) {
				console.log('预约类型', e)
			},
			// 删除图片
			deletePic(event) {
				console.log('删除', event)
				this.fileList1.splice(event.index, 1)
			},
			// 新增图片
			afterRead(event) {
				console.log('新增', event)
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				this.fileList1 = lists
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uniCloud.uploadFile({
						filePath: url, //要上传的文件对象
						cloudPath: Date.now() + '.jpg', //保存在云端的文件名，这里以时间戳命名
						success(res) {
							console.log(3, res.fileID)
							// let imageUrl = res.fileID //云端返回的图片地址
							resolve(res.fileID)
						},
						fail(err) {
							console.log(err)
						},
						complete() {
							uni.hideLoading()
						}
					});
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.add_shop {
		// width: 100%;
		height: 100%;
		margin: 40rpx 52rpx;

		.form_item {
			// width: 100%;
			// height: 68rpx;
			display: block;
			align-items: center;
			margin-bottom: 32rpx;
			font-size: 28rpx;
			// border: 2rpx solid #333333;

			.form_item_box {
				height: 100%;
				display: flex;
				align-items: center;

				text {
					flex: 1.5;
					margin-right: 32rpx;
				}

				input,
				picker{
					flex: 4;
					height: 68rpx;
					background: #EFF4F7;
					border-radius: 6px;
				}

				.row {
					position: absolute;
					right: 20rpx;
					height: 14rpx;
					width: 22rpx;
				}

				image {
					position: absolute;
					right: 20rpx;
					height: 30rpx;
					width: 30rpx;
				}
			}

			.upload_box {
				flex-direction: column;
				align-items: flex-start;

				.upload_title {
					// margin-top: 20rpx;
					margin-bottom: 20rpx;
				}
			}
			/deep/.u-checkbox-group--row {
				flex-wrap: wrap;
				flex-shrink: 1;
			}
			/deep/.u-checkbox-group{
				flex-shrink: 1;
				flex: 4;
			}
			/deep/.u-checkbox {
				width: 129rpx;
				height: 50rpx;
				margin-bottom: 0rpx !important;
				margin-right: 20rpx !important;
			}
			/deep/.u-textarea{
				flex: 4;
			}
		}

		.upload_form_item {
			margin-bottom: 0rpx;
			// height: var(--pHeight);
		}

		// 提交按钮
		.form_btn {
			display: block;
			border: none;
			// width: 630rpx;
			height: 84rpx;
			font-size: 32rpx;
			text-align: center;
			font-weight: 550;
			line-height: 84rpx;
			color: #FFFFFF;
			background-color: $uni-bg-color;
			border-radius: 20rpx;
			// box-shadow: 10px 10px 5px #888888;
			margin-bottom: 40rpx;
			// margin-top: 48rpx;

			&::after {
				border: none
			}
		}
	}
</style>
